package com.mdz.nc.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mdz.nc.Entity.SysMc;
import com.mdz.nc.Entity.SysNc;
import com.mdz.nc.Mapper.SysMcMapper;
import com.mdz.nc.Mapper.SysNcMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孟德志
 * @since 2020-04-03
 */
@Controller
@RequestMapping("/nc/sysNc")
public class SysNcController {

    @Autowired
    private SysNcMapper sysNcMapper;

    @Autowired
    private SysMcMapper sysMcMapper;

    @Autowired
    private HttpServletRequest request;

    @ResponseBody
    @RequestMapping(value = "/getsysnc/{id}", method = RequestMethod.GET)
    public SysNc getspkfk(@PathVariable("id") String id, ModelMap map) {
        SysNc sysNc = sysNcMapper.selectbyid(id);
        map.put("sysNc", sysNc);
        return sysNc;
    }

    @ResponseBody
    @PostMapping("/pageInfospkfk")
    public List<SysNc> pageInfospkfk(@RequestParam(value = "pn",defaultValue = "1") Integer pn) {
        PageHelper.startPage(pn, 10);
        List<SysNc> sysNcList = sysNcMapper.selectList(null);
        PageInfo page=new PageInfo(sysNcList,5);
        return sysNcList;
    }

    @ResponseBody
    @GetMapping("/getsysNc")
    public String getsysNc(){
        List<SysNc> list = sysNcMapper.selectList(null);
        int total = list.size();
        String jsonStr= JSONArray.toJSON(list).toString();
        String jsonString="{\"total\":"+total+",\"totalNotFiltered\":"+total+",\"rows\":"+jsonStr+"}";
        //System.out.println(jsonStr);
//        PageInfo<SysNc> pageInfo=new PageInfo<SysNc>(list,5);
//        map.put("sysNc",pageInfo);
        return jsonString;
    }

    @ResponseBody
    @PostMapping("/sysNctojson")
    public String sysNctojson(){
        List<SysNc> list = sysNcMapper.selectList(null);
        int total = list.size();
        String jsonStr= JSONArray.toJSON(list).toString();
        String jsonString="{\"total\":"+total+",\"totalNotFiltered\":"+total+",\"rows\":"+jsonStr+"}";
        //System.out.println(jsonStr);
//        PageInfo<SysNc> pageInfo=new PageInfo<SysNc>(list,5);
//        map.put("sysNc",pageInfo);
        return jsonString;
    }

    @RequestMapping("/getUserListPage")
    @ResponseBody
    public List<SysNc> getUserListPage(SysNc sysNc, HttpServletRequest request){
        List<SysNc> list = sysNcMapper.getUserListPage(sysNc);
        return list;
    }

    @RequestMapping("/list")
    public String list(ModelMap map){
        List<SysMc> sysMcs=sysMcMapper.selectList(null);
        map.put("sysmcs",sysMcs);
        return "query";
    }

    @ResponseBody
    @RequestMapping(value = {"/getnclist/{mkids}","/getnclist"},method = RequestMethod.GET)
    public String getnclist(@RequestParam int pageNumber, @RequestParam int pageSize, HttpServletResponse response,String went,@PathVariable(value = "mkids",required = false) String mkids){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<String> del_ids = new ArrayList<>();
        String[] str_spids = mkids.split(",");
        for (String string : str_spids) {
            del_ids.add(string);
        }
        List<SysNc> ncList=sysNcMapper.selectList(new QueryWrapper<SysNc>().like("went",went).in("mkid",del_ids));
        PageInfo page=new PageInfo(ncList);
        List<SysNc> rows = page.getList();
        int total = (int) page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/getncall",method = RequestMethod.GET)
    public String getncall(@RequestParam int pageNumber, @RequestParam int pageSize, HttpServletResponse response,SysNc sysNc){
        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PageHelper.startPage(pageNumber, pageSize);
        List<SysNc> ncList=sysNcMapper.selectList(null);
        PageInfo page=new PageInfo(ncList);
        List<SysNc> rows = page.getList();
        int total = (int) page.getTotal();
        JSONObject result = new JSONObject();
        result.put("rows",rows);
        result.put("total",total);
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "/editspkfk/{wtid}", method = RequestMethod.PUT)
    public Integer editspkfk(SysNc sysNc) {
        return sysNcMapper.update(sysNc, new UpdateWrapper<SysNc>().eq("id", sysNc.getId()));
    }

    @ResponseBody
    @RequestMapping(value = "/deletespkfk/{id}", method = RequestMethod.DELETE)
    public Integer deletenc(@PathVariable("id") String id){
        return sysNcMapper.delete(new QueryWrapper<SysNc>().eq("id", id));
    }

    @ResponseBody
    @RequestMapping(value = "/insertspkfk", method = RequestMethod.POST)
    public int insertspkfk(SysNc sysNc) {
        Integer i = sysNcMapper.insertsysNC(sysNc);
        return i;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteallspkfk/{ids}", method = RequestMethod.DELETE)
    public Integer deleteallspkfk(@PathVariable("ids") String ids) {
        List<String> del_ids = new ArrayList<>();
        String[] str_spids = ids.split(",");
        for (String string : str_spids) {
            del_ids.add(string);
        }
        return sysNcMapper.delete(new QueryWrapper<SysNc>().in("id", del_ids));
    }
}

